Repository navigation
fix(streaming): restore estimates when provider usage is absent - #45443
Closed
parkjohnston wants to merge 1 commit into
Closed
parkjohnston wants to merge 1 commit into
parkjohnston wants to merge 1 commit into
Conversation
Only attach aggregated hidden usage when a stream chunk reports usage. Keep final hidden parameters registered so estimates reach callers after stream exhaustion, while preserving explicit zero and late provider counts Adapted from Zuraiz Anjum's fix in BerriAI#44230. Adds regression coverage for sync and async streams, missing and reported usage, and separate, combined, or absent finish chunks Co-authored-by: Zuraiz Anjum <zuraizwork@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
parkjohnston
marked this pull request as ready for review
October 8, 2026 21:22
Contributor
|
Contributor
Author
|
Resolves #45422 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
6 of 7 tasks
Contributor
|
Closed as superseded by #44230, which is merged and restores the token estimate for streams without usage. Thanks for the fix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a developer streaming chat completions through a deployment whose backend sends no usage object sees every request logged at 0 tokens
"stream": trueand nostream_optionsto a model whose backend streams no usage objectusagefield, the same bytes v1.103.1 returnedprompt_tokens: 0, completion_tokens: 0, spend: 0.0After: the same request is logged with the token estimate v1.103.1 produced
"stream": trueand nostream_optionsto a model whose backend streams no usage objectusagefield, the same bytes v1.103.1 returnedprompt_tokens: 16, completion_tokens: 58and the model's priced spendRelevant issues
Fixes #45422
Affected release
regression in v1.104.0 (#42323 made the chunk builder keep the placeholder 0/0 instead of estimating); on main, #44318 (unreleased) widens it to every stream finish shape
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Both legs: one proxy booted with
--num_workers 2from the named commit on the same config and database, one virtual key per leg from POST /key/generate, every request streamed with nostream_options, and the spend row read back through GET /spend/logs?request_id=. Backend with no usage in its stream: a local Ollama 0.40.1 servingqwen3:0.6bthrough its OpenAI-compatible endpoint, which sends no usage object unless the request asks for it. It is registered under thenvidia_nim/prefix, which the proxy adds nostream_optionsfor, so the request reaches the backend with none (the same shape as any backend that ignoresinclude_usageor a client that never sends it). Control: the same backend under theopenai/prefix, where the proxy addsinclude_usageand Ollama reports usage. Hosted case: NVIDIA NIMz-ai/glm-5.3-flash, a priced model whose stream carries no usage object unless askedGitHub Copilot CLI 1.0.94 (the reporter's client) is launched as
copilot --model local-qwen3withCOPILOT_PROVIDER_BASE_URL=http://127.0.0.1:$PORT/v1,COPILOT_PROVIDER_TYPE=openai, andCOPILOT_PROVIDER_API_KEY=<that leg's virtual key>, driven in a terminal with the prompt "Reply with one short sentence about the sea, no tools.", and its rows read with GET /spend/logs?api_key=Before (4ed0267)
POST /v1/chat/completions, stream with no usage object (local-qwen3)
usage, last data line{"id":"chatcmpl-436",...,"choices":[{"finish_reason":"length","index":0,"delta":{}}]}chatcmpl-436andchatcmpl-446:"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0, "spend": 0.0POST /v1/chat/completions, stream that reports usage (local-qwen3-openai)
"model":"local-qwen3-openai": 200, 60 chunkschatcmpl-372:"prompt_tokens": 19, "completion_tokens": 60, "total_tokens": 79POST /v1/chat/completions, hosted NIM stream with no usage object (nim-glm-5.3-flash)
"model":"nim-glm-5.3-flash": 200, 26 chunks, 0 namingusage, last data line{"id":"chatcmpl-cf07901b-...","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}chatcmpl-cf07901b-32ff-4ef9-bc37-c0f31109aca1:"prompt_tokens": 0, "completion_tokens": 0, "spend": 0.0on a priced modelPOST /v1/messages (local-qwen3)
{"model":"local-qwen3","stream":true,"max_tokens":60,"messages":[...]}: 200, 63 events ending in{"type": "message_stop"}msg_0006c199-769c-4099-986d-e0394b48974d:"prompt_tokens": 16, "completion_tokens": 58(this route estimated already)POST /v1/responses (local-qwen3)
{"model":"local-qwen3","stream":true,"max_output_tokens":60,"input":"..."}: 200, 66 events ending inresponse.incompletechatcmpl-559(call_type: aresponses):"prompt_tokens": 16, "completion_tokens": 58(this route estimated already)GitHub Copilot CLI through local-qwen3
include_usageitself, so Ollama reports usage: spend rowchatcmpl-138"prompt_tokens": 5746, "completion_tokens": 105(real counts; the zero needs a stream with no usage object)After (83daa6e)
POST /v1/chat/completions, stream with no usage object (local-qwen3)
usage, last data line{"id":"chatcmpl-247",...,"choices":[{"finish_reason":"length","index":0,"delta":{}}]}chatcmpl-247andchatcmpl-256:"prompt_tokens": 16, "completion_tokens": 58, "total_tokens": 74, the estimate v1.103.1 loggedPOST /v1/chat/completions, stream that reports usage (local-qwen3-openai)
"model":"local-qwen3-openai": 200, 60 chunkschatcmpl-12:"prompt_tokens": 19, "completion_tokens": 60, "total_tokens": 79, unchangedPOST /v1/chat/completions, hosted NIM stream with no usage object (nim-glm-5.3-flash)
"model":"nim-glm-5.3-flash": 200, 16 chunks, 0 namingusagechatcmpl-155b80b0-f958-4ee3-9132-2213d93716fc:"prompt_tokens": 16, "completion_tokens": 34, "total_tokens": 50POST /v1/messages (local-qwen3)
{"type": "message_stop"}msg_eb530019-4127-4e90-a435-80145fbeee0d:"prompt_tokens": 16, "completion_tokens": 58, unchangedPOST /v1/responses (local-qwen3)
response.incompletechatcmpl-432(call_type: aresponses):"prompt_tokens": 16, "completion_tokens": 58, unchangedGitHub Copilot CLI through local-qwen3
chatcmpl-733"prompt_tokens": 5743, "completion_tokens": 107, still the provider's reported countsObservations from the run:
Type
🐛 Bug Fix
Final Attestation